package leetcode.code1161;

import java.util.LinkedList;
import java.util.Queue;

import leetcode.IDebug;
import leetcode.helper.tree.TreeNode;

//执行用时：8 ms, 在所有 Java 提交中击败了83.96%的用户
//
//内存消耗：44.7 MB, 在所有 Java 提交中击败了5.12%的用户
//
//通过测试用例：40 / 40
public class Solution implements IDebug {

	public int maxLevelSum(TreeNode root) {
		int ans = 1;
		int max = Integer.MIN_VALUE;
		Queue<TreeNode> queue = new LinkedList<>();
		queue.add(root);
		int level = 1;
		while (!queue.isEmpty()) {
			int size = queue.size();
			int total = 0;
			while (size > 0) {
				TreeNode cur = queue.poll();
				total += cur.val;
				if (cur.left != null) {
					queue.add(cur.left);
				}
				if (cur.right != null) {
					queue.add(cur.right);
				}
				size--;
			}
			if (total > max) {
				max = total;
				ans = level;
			}
			level += 1;
		}
		return ans;
	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
